var config = require('../../utils/config.js');
var netutil = require('../../utils/netutil.js');
var baseutil = require('../../utils/baseutil.js');
let { WeToast } = require('../../src/wetoast.js');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 1,//0表示火车票   1表示国内机票
    "hidden": true,
    showSearchButton: false,//是否要展示搜索界面
    defaultVaule: '',
    isfromcity: 1,//1出发城市，0到达城市
    sortCity: null,//排序数据
    hotcity: null,//热门城市
    AllData: new Array()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type,
      isfromcity: options.isfromcity
    })
    baseutil.log("this.data.type=" + this.data.type)

    //创建可重复使用的toast实例，并附加到this上，通过this.wetoast访问
    new app.WeToast()


    var THIS = this
    var url = null
    if (this.data.type == 1) {//国内机票
      url = config.getBaseUrl("FlightApi", "GetFlightCityList");
    } else {
      url = config.getBaseUrl("Train", "GetTrainCityList");
    }
    baseutil.log(url);
    netutil.post(
      url, "",
      function (res) {
        if (res.data.ResponseMetaInfo.StatusCode == '0') {
          baseutil.log(res.data);
          var Data = null;
          if (THIS.data.type == 1) {
            Data = res.data.Data.All;
          } else {
            Data = res.data.Data.List;
          }

          var compare = function (obj1, obj2) {
            var val1 = THIS.data.type == 1 ? obj1.CityPinYin : obj1.PinYin;
            var val2 = THIS.data.type == 1 ? obj2.CityPinYin :obj2.PinYin;
            return val1.localeCompare(val2);
          }
          var resultArray = Data.sort(compare);
          var sortCity = new Array();

          var AA = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
            'W', 'X', 'Y', 'Z'];
          var aa = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
            'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z'];

          for (var i = 0; i < AA.length; i++) {
            sortCity[i] = new Array();
          }
          for (var i = 0; i < sortCity.length; i++) {
            for (var j = 0; j < resultArray.length; j++) {
              var a = THIS.data.type == 1 ? resultArray[j].CityPinYin.substring(0, 1) : resultArray[j].PinYin.substring(0, 1)
              if (a == aa[i]) {
                sortCity[i].push(resultArray[j]);
              }
            }
          }
          THIS.setData({ sortCity: sortCity, AA: AA, aa: aa, hotcity: res.data.Data.Hot, AllData: Data })

          baseutil.log(Data);

        } else {
          THIS.wetoast.toast({
            title: res.data.ResponseMetaInfo.Reason,
            duration: 1000
          })
        }
      },
      function (res) {

      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeStart: function (e) {
    var id = e.currentTarget.dataset.pageindex;
    if (id == "1") {
      id = "热门"
    }
    this.setData({
      showwords: id,
      trans: ".3",
      hidden: false
    })
    console.log(id + "---")
  },
  changeEnd: function () {
    var scrollTopId = this.data.showwords == "热门" ? "hot" : this.data.showwords
    this.setData({
      trans: "0",
      hidden: true,
      scrollTopId: scrollTopId
    })
    baseutil.log(this.data.scrollTopId + "=scrollTopId")
  },
  selectCity: function (e) {
    var selectData = null
    if (this.data.showSearchButton){
      
      var position = parseInt(e.currentTarget.dataset.pageindex);
      selectData = this.data.searchData[position]
    }else{
    if (e.currentTarget.dataset.type == "hot") {
        var position = parseInt(e.currentTarget.dataset.pageindex);
        selectData = this.data.hotcity[position]
      } else {
        var shouzimuindex = parseInt(e.currentTarget.dataset.shouzimuindex);
        var position = parseInt(e.currentTarget.dataset.pageindex);
        selectData = this.data.sortCity[shouzimuindex][position]
      }
    }
    
    console.log(selectData)

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
//  * typeStr:'0',国内出发;'1'，国内到达;'2',火车票出发；'3',火车票到达.
    if (this.data.isfromcity == 1) {//出发城市
      if (this.data.type == 1) {//国内机票
        prevPage.updateSelectdCity(selectData,'0')
      } else {//火车票
        prevPage.updateSelectdCity(selectData,'2')
      }
    } else {//目的城市
      if (this.data.type == 1) {//国内机票
        prevPage.updateSelectdCity(selectData,'1')
      } else {//火车票
        prevPage.updateSelectdCity(selectData,'3')
      }
    }
    baseutil.returnLastPage();
  },

  toSearchButton: function () {
    this.setData({ showSearchButton: false, defaultVaule: '', })
  },
  getSearchText(e) {
    var searchText = e.detail.value
    baseutil.log(searchText)
    var AllData = this.data.AllData;
    var searchData = new Array();
    baseutil.log(AllData)
    for (var i = 0; i < AllData.length; i++) {
      var cityData = AllData[i]
      if(this.data.type==1){
        if (baseutil.isContains(cityData.CityName, searchText)
          || baseutil.isContains(cityData.CityPinYin, searchText)
          || baseutil.isContains(cityData.CityShortChar, searchText))
          searchData.push(cityData);
      }else{
        if (baseutil.isContains(cityData.Name, searchText)
          || baseutil.isContains(cityData.PinYin, searchText)
          || baseutil.isContains(cityData.Abbreviation, searchText))
          searchData.push(cityData);
      }
     
    }
    baseutil.log(searchData)
    this.setData({ searchData: searchData })
  },
  showSearch(){
    this.setData({ showSearchButton:true})
  },
 
})